package com.it.test;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
public class RequestAttribute {

    /**
     *原生创建request共享域
     * @param request
     */
    @RequestMapping("/test1")
    public void set(HttpServletRequest request) {
        request.setAttribute("msg", "xiaoxin");
    }


    /**
     * model创建request共享域
     * @param model
     * @return
     */
    @RequestMapping("test2")
    public String add(Model model) {
        model.addAttribute("msg","xiaoxin");
        return "forward:/test01.jsp";
    }


    /**
     * ModelAndView创建request共享域
     * @return
     */
    @RequestMapping("test3")
    public ModelAndView add() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg","xiaoxin");
        mv.setViewName("forward:/test01");
        return mv;
    }


    /**
     * 获取request共享域信息
     * @param request
     * @return
     */
    @RequestMapping("test4")
    public String get(HttpServletRequest request) {
        Object msg = request.getAttribute("msg");

        return "forward:/test01.jsp";
    }
}
